/**
 * Created by JetBrains WebStorm.
 * User: darwmh0d
 * Date: 6/4/14
 * Time: 2:24 PM
 * To change this template use File | Settings | File Templates.
 */

function AdminTreeToConfigVO(rawData) {
    this.rawData = rawData;
    this.groupsTags =  ["type","text"];
    this.selectorsTags = [ "type", "value", "text"];
    this.propertiesTags = [ "title", "type", "property", "value", "label",
        "component", "selectionType", "options", "isNumric"];

    AdminTreeToConfigVO.prototype.GetConfigDataProvider = function(){
        var result = [];
        if(this.rawData !== undefined)
        {
            if(this.rawData != 'undefined')
            {
                var data = this.rawData.children;
                //Getting Groups
                result = GetList(data, this.groupsTags, this.selectorsTags,this.propertiesTags);
            }
        }
        return result;
    }
}

function GetList(data, groupTags, selectorTags, propTags)
{
    var result = [];
    for(var x = 0; x < data.length; x++)
    {
        var group = GetObject(data[x].data, groupTags);
        var selectors = GetSelectors(data[x].children, selectorTags, propTags);
        group['children'] = selectors;
        result.push(group);
    }
    return result;
}

function GetSelectors(data, tags, propTags)
{
    var result = [];
    for(var x = 0; x < data.length; x++)
    {
        var selector = GetObject(data[x].data, tags);
        var properties = GetProperties(data[x].children, propTags);

        selector['children'] = properties;
        result.push(selector);
    }

    return result;
}

function GetProperties(data, tags)
{
    var result = [];
    for(var x = 0; x < data.length; x++)
    {
        result.push(GetObject(data[x].data, tags));
    }

    return result;
}


// Objects
function GetObject(data, tags) {
    var  result = {};
    if(data != 'undefined')
    {
        if(tags !== undefined)
        {
            for (var prop in data) {
                {
                    if(tags.indexOf(prop) != -1)
                        result[prop] = data[prop];
                }
            }
        }

        else
        {
            for (var prop in data) {
                {
                    result[prop] = data[prop];
                }
            }
        }
    }
    return result;
}


